package com.qbb.client.constant;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.qbb.client.bean.services.QbbAttrBean;
import com.qbb.client.bean.services.QbbElementBean;
import com.qbb.client.bean.services.QbbRootBean;
import com.qbb.client.exception.NettyException;
import com.qbb.client.util.FileUtil;

/**
 * 类说明
 * 
 * @author chenhuaiao
 * @version V1.0 创建时间：Mar 17, 2015 5:56:55 PM
 */
public class QbbConfigUtil {
	private static Log log = LogFactory.getLog(QbbConfigUtil.class);
	private static final String rootPath = QbbConfigUtil.class.getResource("/")
			.getPath();
	// 配置参数集合
	private Map<String, QbbAttrBean> serviceMap = new ConcurrentHashMap<String, QbbAttrBean>();
	// 单例模式
	private static QbbConfigUtil singleton;

	private QbbConfigUtil() {
		initClientConfig();
	}

	private static QbbConfigUtil getSineleton() {
		if (singleton == null) {
			synchronized (QbbConfigUtil.class) {
				if (singleton == null) {
					singleton = new QbbConfigUtil();
				}
			}
		}
		return singleton;
	}

	/**
	 * 通过服务名称获得服务对象的参数信息
	 * 
	 * @param serverName
	 * @return
	 */
	public static QbbAttrBean getProper(String serverName) {
		QbbAttrBean attrBean = getSineleton().serviceMap.get(serverName);
		return attrBean;
	}

	private void initClientConfig() {
		QbbRootBean[] models = loadQbbConfig();
		if (models == null) {
			throw new NettyException("客户端文件夹[qbb-client]没有添加关联的服务器接口配置文件!");
		}
		setServiceMap(models);
	}

	private void setServiceMap(QbbRootBean[] models) {
		int modelLen = models.length;

		for (int i = 0; i < modelLen; i++) {
			QbbRootBean model = models[i];
			QbbAttrBean bean = model.getAttrBean();
			List<QbbElementBean> beans = model.getBeans();
			if (beans == null) {
				continue;
			}
			int beansSize = beans.size();
			for (int j = 0; j < beansSize; j++) {
				String serviceName = beans.get(j).getName();
				serviceMap.put(serviceName, bean);
			}
		}
	}

	private static QbbRootBean[] loadQbbConfig() {
		String dictionaryPath = rootPath + "qbb-client";
		List<File> files = FileUtil.getPathFileNames(dictionaryPath);
		if (files == null) {
			throw new NettyException("客户端文件夹[qbb-client]没有添加关联的服务器接口配置文件!");
		}

		int fileSize = files.size();

		QbbRootBean[] allServerModels = new QbbRootBean[fileSize];

		// 创建saxReader对象
		SAXReader reader = new SAXReader();

		try {
			for (int i = 0; i < fileSize; i++) {
				File file = files.get(i);

				Document document = reader.read(file);

				Element node = document.getRootElement();

				Element ipElement = node.element("ip");
				String ipStr = ipElement.getText().trim();

				Element portElement = node.element("port");
				String portStr = portElement.getText().trim();
				int port = Integer.parseInt(portStr);

				QbbAttrBean attrbean = new QbbAttrBean();
				attrbean.setIp(ipStr);
				attrbean.setPort(port);

				List<QbbElementBean> serviceBeans = new ArrayList<QbbElementBean>();

				QbbRootBean rootBean = new QbbRootBean();
				rootBean.setAttrBean(attrbean);
				rootBean.setBeans(serviceBeans);

				allServerModels[i] = rootBean;

				Iterator<Element> servicesElement = node.elementIterator();
				// 遍历
				while (servicesElement.hasNext()) {
					// 获取某个子节点对象
					Element service = servicesElement.next();
					// 对子节点进行遍历
					Attribute attr = service.attribute("name");
					if (attr == null) {
						continue;
					}
					String serviceName = attr.getValue().trim();

					QbbElementBean serviceBean = new QbbElementBean();
					serviceBean.setName(serviceName);
					serviceBeans.add(serviceBean);
				}

			}
			return allServerModels;
		} catch (Exception e) {
			throw new NettyException(
					"QbbClient load xml config file have exception:qbb-client文件夹内,创建的服务接口配置文件不能为空!");
		}
	}
}
